package ipad.asia.controller;


import ipad.asia.entity.Consumer;
import ipad.asia.entity.Order;
import ipad.asia.entity.Product;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.order.OrderFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.utils.MD5Util;
import ipad.asia.utils.MyTime;
import ipad.asia.utils.RandomNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class BuyController {
    @Autowired
    private ConsumerFeign consumerFeign;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private MyTime myTime;
    @Autowired
    private RandomNum randomNum;
    @RequestMapping("/buy")
    public String buy(HttpServletRequest request, Model model, HttpServletResponse response,
                      @RequestParam(required = false,value = "productId")String productId,
                      @RequestParam(required = false,value = "price")String price) throws UnsupportedEncodingException {
        Product product=productFeign.getById(Integer.parseInt(productId));
        System.out.println("产品id"+product.getId());
        if(product.getNumber()<1){
            model.addAttribute("message","当前商品已售光！");
            return "buy";
        }else{
            Consumer user1 = consumerFeign.getName((String) request.getSession().getAttribute("user"));
            List<Order> orders = orderFeign.findByConsumerId(user1.getId());
            for (int i = 0; i < orders.size(); i++) {
                System.out.println(orders.get(i).getProductId().equals(product.getId()));
                System.out.println(orders.get(i).getStatues().equals(0));
                System.out.println("订单状态："+orders.get(i).getStatues());
                System.out.println("订单id："+orders.get(i).getProductId());
                System.out.println(orders.get(i).getProductId()==product.getId()&&orders.get(i).getStatues()==0);
                if(orders.get(i).getProductId().equals(product.getId())&&orders.get(i).getStatues().equals(0)){
                    model.addAttribute("message","你已有未付款的这款商品！");
                    return "redirect:/order/noPay";
                }
            }
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=UTF-8");
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
            HttpSession session = request.getSession();
            String user = (String) session.getAttribute("user");
            Consumer consumer = consumerFeign.getName(user);
            Product products = productFeign.getById(Integer.parseInt(productId));
//            int rand1 = randomNum.RandomNumber1();
//            int rand2 = randomNum.RandomNumber2();
//            int rand3 = randomNum.RandomNumber2();
//            String date = (String.valueOf(new SimpleDateFormat("ss").format(new Date())));
            //三次随机数相加确保订单号不会重复！取日期数字过长不适用int，用long会有精度问题。
//            String orderId = rand1 + rand2 + rand3 + date;
//            System.out.println("orders--------" + orderId);
//            System.out.println("user----" + user);
//            System.out.println("products-----" + products);
//            System.out.println("price----------" + price);
//            System.out.println("productname-----" + products.getName());

            model.addAttribute("products", products);
            model.addAttribute("price", Integer.parseInt(price));
            model.addAttribute("consumer", consumer);
//            System.out.println("地址：" + consumer.getAddress());
//            System.out.println("时间" + myTime.LocalDateTime());
            return "buy";
//            List<String> orderIds = new ArrayList<>();

//            for (int i = 0; i < orders.size(); i++) {
//                if(orders.get(i).getStatues()!=0){
//                    orderIds.add(orders.get(i).getId() + "");
//                }
//            }
//            System.out.println("除了未付款的orderid");
//            if (orderIds.contains(inputOrderId)) {
//                model.addAttribute("oderId", inputOrderId);
//                return "buy";
//            } else {



//                model.addAttribute("oderId", orderId);
//                orderFeign.orderAdd(Integer.parseInt(orderId), consumer.getId(), Float.parseFloat(price), Integer.parseInt(productId),
//                        1, order_bbs, products.getShopId(), consumer.getAddress(), 0, new Date(), null);
//                //修改库存数量
//                productFeign.updateNumber(product.getNumber() - 1, Integer.parseInt(productId));

//            }
        }
    }

    @RequestMapping("/orderToBuy")
    public String orderToBuy(HttpServletRequest request, Model model, HttpServletResponse response,
                      @RequestParam(required = false,value = "productId")String productId,
                      @RequestParam(required = false,value = "price")String price,
                      @RequestParam(required = false,value = "orderId")String orderId,
                      @RequestParam(required = false,value = "order_bbs")String order_bbs) throws UnsupportedEncodingException {
//        Product product=productFeign.getById(Integer.parseInt(productId));
            System.out.println("留言：" + order_bbs);
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=UTF-8");
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
            HttpSession session = request.getSession();
            String user = (String) session.getAttribute("user");
            Consumer consumer = consumerFeign.getName(user);
            Product products = productFeign.getById(Integer.parseInt(productId));
//            int rand1 = randomNum.RandomNumber1();
//            int rand2 = randomNum.RandomNumber2();
//            int rand3 = randomNum.RandomNumber2();
//            String date = (String.valueOf(new SimpleDateFormat("ss").format(new Date())));

//            System.out.println("orders--------" + orderId);
//            System.out.println("user----" + user);
//            System.out.println("products-----" + products);
//            System.out.println("price----------" + price);
//            System.out.println("productname-----" + products.getName());

            model.addAttribute("products", products);
            model.addAttribute("price", Integer.parseInt(price));
            model.addAttribute("consumer", consumer);
            System.out.println("地址：" + consumer.getAddress());
            System.out.println("时间" + myTime.LocalDateTime());
//            List<String> orderIds = new ArrayList<>();
                model.addAttribute("oderId", orderId);

                //修改库存数量
                return "buy";
    }
}
